#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<string>
#include<stdio.h>
using namespace std;
int main() {
	int n, q;
	cin >> n >> q >> " ";
	const int maxn = 20;
	string a, b, c, d, e;
	string t[maxn], da[maxn];
	for (int i = 0; i < n; i++) {
		cin >> t[i] >> da[i] >> " ";
	}
	for (int i = 0; i < q; i++) {
		int flag;
		cin >> a >> b >> c >> d >> e >> " ";
		for (int j = 0; j < q; j++) {
			if (a == t[j]) {
				flag = j;
				break;
			}
		}
		if (da[flag] == b) {
			cout << "A";
		}
		else if (da[flag] == c) {
			cout << "B";
		}
		else if (da[flag] == d) {
			cout << "C";
		}
		else(da[flag] == e); {
			cout << "D";
		}
	}
	return 0;
}
